<?php
require_once '../library/Util/Util.php';
require_once '../library/Util/OAuth2.php';
class IndexController extends Zend_Controller_Action{

	public function init(){
		parent::init();
		$request = $this->getRequest();
		$action = $request->getActionName();
		$uid = $request->getParam('uid', null);

		if($action == 'index'){		
			if(empty($uid) && !empty($_SESSION['logginUser'])){
				unset($_SESSION['logginUser']);
			}else if(!empty($uid) && ( (empty($_SESSION['logginUser']) || ($_SESSION['logginUser']['uid'] != $uid)))){
				unset($_SESSION['logginUser']);
				$result = Util::ssoLogin('http://localhost:3000/sso', '/index/auth',array('nickname', 'fullname'), array('email'));
				if($result != ""){
					$_SESSION['msg']['action'] = 'loginfail';
					$this->_redirect('/error');							
				}
				exit();			
			}else if(!empty($uid) && !empty($_SESSION['logginUser'])){
				$check = Util::ssoCheckState('http://localhost:3000/sso', $_SESSION['logginUser']['uid'], $_SESSION['logginUser']['sid']);
				if(!$check){
					unset($_SESSION['logginUser']);
				}
			}
		}	
	}

	public function indexAction(){
		
		/*
		 * Khai bao cho header
		 */
		$this->view->headTitle()->append('Blog');
		$this->view->headLink()->appendStylesheet('/css/bootstrap.min.css','screen',true);
		$this->view->headLink()->appendStylesheet('/css/bootstrap-responsive.min.css','screen',true);
		$this->view->headLink()->appendStylesheet('/css/style.css','screen',true);

		$this->view->headScript()->appendFile('/js/jquery-1.8.3.min.js');	
		$this->view->headScript()->appendFile('http://localhost:3000/js/soCore.js');
		$this->view->headScript()->appendFile('/js/bootstrap.min.js');

		$tbBlogs = new User_Model_Blogs();
		$newBlogs = array();
		$newBlogs = $tbBlogs->getAllNewBlogs();

		foreach ($newBlogs as &$blog) {
			$blog['title'] = Util::split_words($blog['title'], 20, "...");
			$blog['content'] = Util::split_words($blog['content'], 200, "...");
			$blog['fullname'] = '';
		}

		$listUid = array('lst_uid' => '');
		foreach($newBlogs as $blog){
			if(strpos($listUid['lst_uid'], $blog['uid'].',') === false){
				$listUid['lst_uid'] .= $blog['uid'].',';				
			}
		}
		if(!empty($listUid['lst_uid'])){
			$listUid['lst_uid'] = rtrim($listUid['lst_uid'], ",");
		}

		$oauthData = Util::oauthGetApi('http://localhost:3000/api/v1/users', $listUid, 'GET');
		if(!empty($oauthData) && is_array($oauthData)){
			foreach($newBlogs as &$blog){
				if(!empty($oauthData[$blog['uid']])){
					$blog['fullname'] = $oauthData[$blog['uid']]['fullname'];					
				}
			}
		}

		$userBlogs = $tbBlogs->getUserBlogsInPage(1);
		$totalBlogs = $tbBlogs->countAllUserBlogs(1);
		$topBlogger = $tbBlogs->getTopBlogger();

		$listUid = array('lst_uid' => '');
		foreach($topBlogger as $blogger){
			$listUid['lst_uid'] .= $blogger['uid'].',';
		}
		if(!empty($listUid['lst_uid'])){
			$listUid['lst_uid'] = rtrim($listUid['lst_uid'], ",");
		}
		$oauthData = Util::oauthGetApi('http://localhost:3000/api/v1/users', $listUid, 'GET');
		if(!empty($oauthData) && is_array($oauthData)){
			$bloggers = $oauthData;
		}else{
			$bloggers = null;
		}
		$this->view->assign('topBlogger', $topBlogger);
		$this->view->assign('bloggers', $bloggers);
		$this->view->assign('newBlogs', $newBlogs);
	}

	public function authAction(){
		$this->_helper->layout()->disableLayout();
		
		$result = Util::ssoComplete('/index/auth', array('nickname', 'fullname'), array('email'));

		if(isset($_SESSION['logginUser'])){
			unset($_SESSION['logginUser']);
		}

		if($result['msg'] == ""){
			$_SESSION['logginUser'] = $result['data'];
		}else{
			$_SESSION['msg']['action'] = 'loginfail';
			$this->_redirect('/error');
		}
	}

	public function callbackAction(){
		$request = $this->getRequest();
		$this->_helper->layout()->disableLayout();
		$code = $request->getParam('code', null);

		if(!empty($code)){
			echo $code; 
		}else{
			$_SESSION['msg']['action'] = 'notfound';
			$this->_redirect('/error');			
		}
	}

}